class Offer < ActiveRecord::Base
  
  has_many :book
  
   validates_presence_of :offer_book_id
   validates_presence_of :for_book_id
   validates_presence_of :status
   
  #To DO
  #write a find method to get all ACTIVE of the offers for books that I own based on my ID
  def self.get_offers_to_me(user_id)
     find_by_sql([" SELECT my_books.id AS my_book_id, my_books.title AS my_book_title, 
 offer_books.id AS offer_book_id, offer_books.title AS offer_book_title
 FROM offers 
join books my_books on offers.for_book_id = my_books.id 
join books offer_books on offers.offer_book_id = offer_books.id 
join users on my_books.user_id = users.id 
WHERE users.id = ?",user_id] )  
    
  end
  #to DO
  #write a find method that will get all ACTIVE of the offers that I have made for other books
  def self.get_offers_from_me(user_id)
    find_by_sql([" SELECT my_books.id AS my_book_id, my_books.title AS my_book_title, 
 offer_books.id AS offer_book_id, offer_books.title AS offer_book_title
 FROM offers 
join books my_books on offers.offer_book_id = my_books.id 
join books offer_books on offers.for_book_id = offer_books.id 
join users on my_books.user_id = users.id 
WHERE users.id = ?",user_id] )  
  end
end
